package apis

import (
	"github.com/gin-gonic/gin"
	"tylnmall/models"
	"common/logger"
	"net/http"
	"tylnmall/constant"
	"github.com/jinzhu/gorm"
	"errors"
)

func ThemeListApi(c *gin.Context){
	var err error
	var Theme []*models.Theme
	db := c.MustGet("ty").(*gorm.DB)
	if err = db.Find(&Theme).Error; err != nil {
		logger.Error(err)
		err = errors.New("theme not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"list": Theme})
}

func ThemeByIdApi(c *gin.Context){
	type param struct {
		Id int `json:"id" form:"id"`
	}
	var p param
	var err error
	var theme models.Theme
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	db := c.MustGet("ty").(*gorm.DB)
	if err = db.Where("id = ?", p.Id).Find(&theme).Error; err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"Theme": theme})
}